import { director } from "cc";
import { SDKDataMod } from "../SDKDataMode";
import { SDKEventDef } from "../SDKEventDef";
import { SDKTools } from "../SDKTools";

export class TaskData {
    // 通过接口获得的数据
    _dat: SDKDataMod.RewardTask = null;

    public SetData(dat: SDKDataMod.RewardTask) {
        this._dat = dat;
    }
    _SaveKey: string = "TaskData";

    public get m_SavKey(): string {
        console.log("TaskDatam_SavKey", this._SaveKey + "_" + this._dat._index);
        return this._SaveKey + "_" + this._dat._index;
    }

    public get m_TaskIndex(): string {
        return this._dat._index;
    }

    public get m_TaskCount(): number {
        return this._dat.ad_task_count;
    }

    public saveData: SDKDataMod.RewardTaskSaveData = null;

    public get m_CheckTaskState(): "完成" | "未完成" {
        if (this.saveData.Count >= this.m_TaskCount) {
            return "完成";
        }
        return "未完成";
    }

    public UpDataTaskCount() {
        this.saveData.Count++;
        this.saveData._index = `${this._SaveKey}_${SDKTools.NowTime}`;
        SDKTools.SavaData(this.m_SavKey, this.saveData);
        director.emit(SDKEventDef.UPDATE_TSAK_INFO);
    }

    public get m_okTime():string{
        let time = parseInt(this.saveData._index.split("_")[1]) * 1000;
        let date = new Date(time)
        let year = date.getFullYear()
        let month = date.getMonth() + 1
        let day = date.getDate()
        let hour = date.getHours()
        let minute = date.getMinutes()
        let second = date.getSeconds()
        return `完成时间:${month}/${day} ${hour}:${minute}:${second}_${SDKTools.skdName}`;
    }

    public get m_TaskFinishCount(): number {
        return this.saveData.Count;
    }

    public Init() {
        this.saveData = SDKTools.GetData(this.m_SavKey, { Count: 0, _index: "" });
        console.log("TaskData", this.saveData);
        if (this.saveData._index != "") {
            let time = parseInt(this.saveData._index.split("_")[1]) * 1000;
            if (!SDKTools.IsToday(time)) {
                // 不是今天
                this.saveData.Count = 0;
                this.saveData._index = "";
            }
        } else {
            // 重来没有做过 或者 清理了缓存
            this.saveData.Count = 0;
        }
        console.log("TaskData2", this.saveData);
        director.emit(SDKEventDef.UPDATE_TSAK_INFO);
    }
}